package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.Bid;
import com.ruoyi.system.service.IBidService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 投标Controller
 * 
 * @author 李演
 * @date 2024-03-27
 */
@RestController
@RequestMapping("/bid")
public class BidController extends BaseController
{
    @Autowired
    private IBidService bidService;

    /**
     * 查询投标列表
     */
    @RequiresPermissions("system:bid:list")
    @GetMapping("/list")
    public TableDataInfo list(Bid bid)
    {
        startPage();
        List<Bid> list = bidService.selectBidList(bid);
        return getDataTable(list);
    }
    @GetMapping("/bidCount")
    public int bidCount(@RequestParam("employerId") Long employerId){
        return bidService.bidCount(employerId);
    }

    /**
     * 导出投标列表
     */
    @RequiresPermissions("system:bid:export")
    @Log(title = "投标", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Bid bid)
    {
        List<Bid> list = bidService.selectBidList(bid);
        ExcelUtil<Bid> util = new ExcelUtil<Bid>(Bid.class);
        util.exportExcel(response, list, "投标数据");
    }

    /**
     * 获取投标详细信息
     */
    @RequiresPermissions("system:bid:query")
    @GetMapping(value = "/{taskId}")
    public AjaxResult getInfo(@PathVariable("taskId") Long taskId)
    {
        return success(bidService.selectBidByTaskId(taskId));
    }

    /**
     * 新增投标
     */
   // @RequiresPermissions("system:bid:add")
    @Log(title = "投标", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Bid bid)
    {
        return toAjax(bidService.insertBid(bid));
    }
    @Transactional
    @PostMapping("/toubiao")
    public AjaxResult addToubiao(@RequestBody Bid bid)
    {
        //投标人数不能超过5人
        Bid tempBidCount = new Bid();
        tempBidCount.setTaskId(bid.getTaskId());
        tempBidCount.setState(0L);
        List<Bid> tempBidList = bidService.selectBidList(tempBidCount);
        if (tempBidList.size()>=5){
            return error("投标人数不能超过5人");
        }
        //判断是否重复投标
        Bid tempBid = new Bid();
        tempBid.setTaskId(bid.getTaskId());
        tempBid.setEmployeeId(bid.getEmployeeId());
        List<Bid> bids = bidService.selectBidList(tempBid);
        if(bids.size()>0){
            return error("重复投标");
        }
        return toAjax(bidService.insertBid(bid));
    }
    //放弃竞标
    @PostMapping("/giveUpBid")
    public AjaxResult giveUpBid(@RequestBody Bid bid)
    {
        return toAjax(bidService.giveUp(bid));
    }

    /**
     * 修改投标
     */
    @RequiresPermissions("system:bid:edit")
    @Log(title = "投标", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Bid bid)
    {
        return toAjax(bidService.updateBid(bid));
    }

    /**
     * 删除投标
     */
    @RequiresPermissions("system:bid:remove")
    @Log(title = "投标", businessType = BusinessType.DELETE)
	@DeleteMapping("/{taskIds}")
    public AjaxResult remove(@PathVariable Long[] taskIds)
    {
        return toAjax(bidService.deleteBidByTaskIds(taskIds));
    }
}
